package com.zang.array;

/**
 * @author ZVerify
 * @since 2022/10/09 17:11
 * @see <a href="https://leetcode.cn/problems/spiral-matrix-ii/submissions/">...</a>
 **/
public class 螺旋矩阵II {

    public int[][] generateMatrix(int n) {

        int[][] ints = new int[n][n];

        int start = 0; // 起始坐标
        int count = 1; // 填充值
        int loop = 0; // 循环次数
        int r = 1;
        int j,i;
        while (loop++<n/2){

            for (j = start;j<n-loop;j++){
                ints[start][j] = count++;
            }

            for (i = start;i<n-loop;i++){
                ints[i][j] = count++;
            }

            for (;j>=loop;j--){
                ints[i][j] = count++;
            }

            for (;i>=loop;i--){
                ints[i][j] = count++;
            }

            start++;
        }

        if (n%2==1){
            ints[start][start] = count;
        }

        return ints;
    }
}
